import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutterapp/Home.dart';
import 'package:flutterapp/PermissionHandlerWidget.dart';
import 'package:flutterapp/login.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(MyApp());
  if (Platform.isAndroid) {
    SystemUiOverlayStyle style = SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,

        ///这是设置状态栏的图标和字体的颜色
        ///Brightness.light  一般都是显示为白色
        ///Brightness.dark 一般都是显示为黑色
        statusBarIconBrightness: Brightness.light
    );
    SystemChrome.setSystemUIOverlayStyle(style);
  }
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme:
          ThemeData(primarySwatch: Colors.blue, primaryColorDark: Colors.blue),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
      routes: <String, WidgetBuilder> {
        '/login': (BuildContext context) => Login(),
        '/home': (BuildContext context) => HomePage(),
        '/demo': (BuildContext context) => PermissionHandlerWidget()
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  final TextEditingController _controller = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        centerTitle: true,
      ),
      body: Center(
        // Center is a layout widget. It takes a single child and positions it
        // in the middle of the parent.
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
            ElevatedButton(
              onPressed: () {
                _alertSimpleDialog();
              },
              child: new Text('DONE'),
            ),
            ElevatedButton(
              child: Text("登录"),
              onPressed: () {
                Navigator.pushNamed(context, '/login');
              },
            ),
            ElevatedButton(
                onPressed: () {
                  Navigator.pushNamed(context, '/demo');
                  // _requestPermissions();
                },
                child: Text("申请权限")),
            // PermissionHandlerWidget()
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  _alertSimpleDialog() async {
    var alertDialogs = await showDialog(
        context: context,
        builder: (context) {
          return SimpleDialog(
            title: Text("提示"),
            children: <Widget>[
              TextButton(
                child: Text("Option1"),
                onPressed: () => Navigator.pop(context, "Option1"),
              ),
              TextButton(
                child: Text("Option2"),
                onPressed: () => Navigator.pop(context, "Option2"),
              ),
              TextButton(
                child: Text("Option3"),
                onPressed: () => Navigator.pop(context, "Option3"),
              ),
            ],
          );
        });
    return alertDialogs;
  }

  Future<bool> _requestPermissions() async {
    // //  申请权限
    var status = await Permission.camera.status;
    if (status.isDenied) {
      status = await Permission.camera.request();
    }
    if (status.isGranted) {
      print("申请成功");
    } else {
      var showReq = await Permission.camera.shouldShowRequestRationale;
      if (!showReq) {
        print('当前权限已被禁用申请，请在设置中手动开启后退出重新进入程序');
        await openAppSettings();
      }
    }
//     Map<Permission, PermissionStatus> permissions = await [
//       Permission.storage,
//       Permission.camera,
//     ].request();
// //  申请结果
//     PermissionStatus permissionStatus = permissions[Permission.camera];

//     // await PermissionHandler().checkPermissionStatus(PermissionGroup.camera);
//     if (permissionStatus == PermissionStatus.granted) {
//       print('申请成功');
//     } else {
//       print('申请被拒绝');
//       bool isShow = await PermissionHandler()
//           .shouldShowRequestPermissionRationale(PermissionGroup.camera);
//       if (!isShow) {
//         print('当前权限已被禁用申请，请在设置中手动开启后退出重新进入程序');
//         await PermissionHandler().openAppSettings();
//       } else {
//         PermissionStatus ps = await PermissionHandler()
//             .checkPermissionStatus(PermissionGroup.camera);
//         if (ps == PermissionStatus.granted) {
//           print('登录成功！');
//         } else {
//           _requestPermissions();
//         }
//       }
//     }
  }
}
